import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DataKeys;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.VirtualFile;
import org.codehaus.plexus.util.FileUtils;

import javax.swing.JFileChooser;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * TODO:请描述本类的作用
 * @version 1.0
 * @update 【1】【2019/1/2 13:07】【hao.z】【创建】
 */
public class FileExportAction extends AnAction {
    private static String LAST_PATH_FILE = System.getProperty("java.io.tmpdir") + "/idea_last_exportpath.dat";

    public static String readLastPath() {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(LAST_PATH_FILE));
            return reader.readLine();
        } catch (Exception e) {
            return System.getProperty("user.home") + File.separator + "Desktop";
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                }
            }
        }
    }

    public static void writeLastPath(String lastPath) {
        PrintWriter writer = null;
        try {
            writer = new PrintWriter(new FileWriter(LAST_PATH_FILE));
            writer.println(lastPath);
        } catch (Exception e) {
        } finally {
            if (writer != null) {
                writer.close();
            }
        }
    }

    @Override
    public void actionPerformed(AnActionEvent event) {
        VirtualFile[] files = event.getData(DataKeys.VIRTUAL_FILE_ARRAY);
        if (files == null || files.length == 0) {
            Messages.showErrorDialog("Please Select Export File!", "Error");
            return;
        }
        JFileChooser fileChooser = new JFileChooser(readLastPath());
        fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int flag = fileChooser.showOpenDialog(null);
        if (flag == JFileChooser.APPROVE_OPTION) {
            String exportPath = fileChooser.getSelectedFile().getAbsolutePath();
            exportFile(event, files, exportPath);
            writeLastPath(exportPath);
        }
    }


    private void exportFile(AnActionEvent event, VirtualFile[] files, String exportPath) {
        try {
            String basePath = event.getProject().getBasePath();
            String proName = event.getProject().getName();
            for (VirtualFile file : files) {
                File destFile = new File(exportPath + "/" + proName + "/" + file.getPath().replaceFirst(basePath, ""));
                File srcFile = new File(file.getPath());
                if (srcFile.isDirectory()) {
                    FileUtils.copyDirectoryStructure(srcFile, destFile);
                } else {
                    FileUtils.copyFile(srcFile, destFile);
                }
            }
            //Messages.showInfoMessage("Exported Success!", "Info");
        } catch (Exception e) {
           throw new RuntimeException(e);
        }
    }
}
